# 导入自动化模块
from DrissionPage import ChromiumPage
import datetime
import pandas as pd


def get_data():
    # 打开浏览器
    driver = ChromiumPage()
    # 监听数据包
    driver.listen.start("aweme/v1/web/comment/list/")
    # 访问网站
    driver.get("https://www.douyin.com/video/7353500880198536457")
    result = []
    for i in range(20):
        print(f"正在采集第{i+1}页数据内容")
        # 下滑页面到底部
        driver.scroll.to_bottom()
        # 等待数据包加载
        resp = driver.listen.wait()
        # 直接返回数据包响应的数据
        json_data = resp.response.body
        # 解析数据
        comments = json_data["comments"]
        for comment in comments:
            text = comment["text"]
            nickname = comment["user"]["nickname"]
            create_time = comment["create_time"]
            date = str(datetime.datetime.fromtimestamp(create_time))
            ip = comment["ip_label"]
            dic = {
                "昵称": nickname,
                "归属地": ip,
                "时间": date,
                "评论": text
            }
            result.append(dic)
            print(dic)
    save(result)


def save(result):
    df = pd.DataFrame(result)
    df.to_excel("comments.xlsx", index=False)


if __name__ == '__main__':
    get_data()